package com.zzyweb.demomp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzyweb.demomp.entity.User;
import com.zzyweb.demomp.mapper.UserMapperPlus;
import com.zzyweb.demomp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * @author zhiyi
 * @date 9/9/25
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapperPlus userMapperPlus;
    @Override
    public List<User> getAll() {
        return userMapperPlus.selectList(new QueryWrapper<User>());
    }

    @Override
    public void register(User user) {
        //密码加密
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        userMapperPlus.insert(user);
    }

    @Override
    public User login(String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username).eq("password", password);
        User user = userMapperPlus.selectOne(wrapper);
        if (user == null) throw new RuntimeException();
        return user;
    }

}
